package cn.service.productService.ProductServiceImpl;

import cn.service.productService.AttributeValueService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import entity.productEntity.AttributeValue;
import entity.Result;
import mapper.productMapper.AttributeValueMapper;
import mapper.productMapper.PmsProductMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 朱颢阳
 * @description
 * @time 2024/8/21 9:43
 */
@Service
public class AttributeValueServiceImpl implements AttributeValueService {
    @Resource
    AttributeValueMapper mapper;
    @Resource
    PmsProductMapper pmsProductMapper;

    @Override
    public Result selsectAll(Integer id, Integer currentPage, Integer pageSize) {
        System.out.println(id);
        PageHelper.startPage(currentPage, pageSize);
        Page<AttributeValue> page = (Page<AttributeValue>) mapper.selectAll(id);
        System.out.println(page.getTotal());
        return Result.success((int) page.getTotal(), page.getResult());
    }

    @Override
    public Result selectAttAll() {
        return Result.success(200, mapper.selectAttAll());
    }

    @Override
    public Result insertAttributevalue(AttributeValue attributeValue) {
        if (mapper.insert(attributeValue) > 0) {
            return Result.success("添加成功");
        } else {
            return Result.failed(500, "添加失败！");
        }
    }

    @Override
    public Result deleteByid(Integer id, Integer attid) {
        if (pmsProductMapper.selectAttProduct(attid) > 0) {
            return Result.failed(500, "该字段还有商品存在！删除失败！");
        } else {
            if (mapper.deleteByPrimaryKey(id) > 0) {
                return Result.success("删除成功");
            } else {
                return Result.failed(500, "删除失败！");
            }
        }
    }

    @Override
    public Result updataAttributeValue(AttributeValue attributeValue) {
        if (mapper.updateByPrimaryKeySelective(attributeValue) > 0) {
            return Result.success("修改成功");
        } else {
            return Result.failed(500, "修改失败！");
        }
    }

    @Override
    public Result getAttributevalues(Integer id) {
        List<AttributeValue> attributevalues = mapper.getAttributevalues(id);
        System.out.println(attributevalues);
        return Result.success(200, attributevalues);
    }
}
